<?php

namespace common\models\datas;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%album}}".
 *
 * @property integer $id
 * @property string $class_name
 * @property integer $object_id
 * @property string $file_path
 * @property string $alt
 * @property integer $created_at
 * @property integer $branch_id
 */
class Album extends \common\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%album}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['object_id', 'created_at', 'branch_id'], 'integer'],
            [['alt'], 'string'],
            [['class_name', 'file_path'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'class_name' => 'Class Name',
            'object_id' => 'Object ID',
            'file_path' => 'File Path',
            'alt' => 'Alt',
            'created_at' => 'Create At',
            'branch_id' => 'Branch ID',
        ];
    }

    //转换时间格式
    public function fields()
    {
        $fields = parent::fields();
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],

        ]);


    }

    public function getPositionInfo()
    {
        return $this->hasOne(PositionInfo::className(), ['id' => 'object_id']);
    }
}
